package com.kui.trytodo.chapter06;

import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class FileUploadController {
    @PostMapping("singleUpload")
    @ResponseBody
    public void upload(MultipartFile file, String username, Integer age) {
        System.out.println("username = " + username);
        System.out.println("age = " + age);
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        System.out.println(file.getContentType());
        System.out.println(file.getName());
        try {
            file.transferTo(new File("E:\\test\\" + file.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("multiUpload")
    @ResponseBody
    public void upload(MultipartFile file1, MultipartFile file2, String username, Integer age) {
        System.out.println("username = " + username);
        System.out.println("age = " + age);
        System.out.println("file1.getOriginalFilename() = " + file1.getOriginalFilename());
        System.out.println("file2.getOriginalFilename() = " + file2.getOriginalFilename());
        try {
            file1.transferTo(new File("E:\\test\\" + file1.getOriginalFilename()));
            file2.transferTo(new File("E:\\test\\" + file2.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("upload3")
    @ResponseBody
    public void upload(MultipartHttpServletRequest request) {
        // 处理非文件
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
            String parameter = stringEntry.getKey();
            String[] value = stringEntry.getValue();
            System.out.println("parameter = " + parameter);
            System.out.println("value = " + value);
        }

        // 处理文件
        MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
        for (Map.Entry<String, List<MultipartFile>> stringListEntry : multiFileMap.entrySet()) {
            List<MultipartFile> value = stringListEntry.getValue();
            for (MultipartFile multipartFile : value) {
                System.out.println("multipartFile.getOriginalFilename() = " + multipartFile.getOriginalFilename());
                try {
                    multipartFile.transferTo(new File("E:\\test\\" + multipartFile.getOriginalFilename()));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @PostMapping("upload4")
    @ResponseBody
    public void upload(FormInput input) {
        String username = input.getUsername();
        System.out.println("username = " + username);
        Integer age = input.getAge();
        System.out.println("age = " + age);

        MultipartFile file1 = input.getFile1();
        System.out.println("file1.getOriginalFilename() = " + file1.getOriginalFilename());
        MultipartFile file2 = input.getFile2();
        System.out.println("file2.getOriginalFilename() = " + file2.getOriginalFilename());
        try {
            file1.transferTo(new File("E:\\test\\" + file1.getOriginalFilename()));
            file2.transferTo(new File("E:\\test\\" + file2.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
